/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false,
        columnClass: 'text-center',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'status',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'userId',
        title: '客户ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'u.userName',
        title: '客户姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left width-100',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'u.mobilePhone',
        title: '电话',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true
    },
    {
        id:'inviterName',
        title:'邀请人姓名',
        type:'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
    },
    {
        id: 'equityCode',
        title: '股票代码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },

    {
        id: 'equityName',
        title: '股票名称',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'tradeCredit',
        title: '点买额度(万)',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'range',
        format: '#,##0'
    },
    {
        id: 'buyPrice',
        title: '点买价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'range',
        format: '#,##0.00'
    },
    {
        id: 'buyNumber',
        title: '股票数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format: '#,##0'
    },
    {
        id: 'profitStopPrice',
        title: '止盈价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'loseStopPrice',
        title: '止损价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'buyTime',
        title: '点买时间',
        type: 'date',
        format: 'yyyy-MM-dd HH:mm:ss',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'status',
        title: '抢单',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: true,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var pwdHtml = "<a class='btn btn-xs btn-primary m-r-10' onclick='_qiangDan(this)' data-no='" + dataNo + "'><i class='fa fa-edit'></i>&nbsp;抢单&nbsp;</a>";
            var html = "";
            var $Permi = $("#scramblePermi");
            // 判断是否有编辑权限
            if ($Permi && $Permi.val() == 1) {
                if (record.status == 1) {
                    html += pwdHtml;
                }
            }
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'order/list.json',
    exportFileName: '方案列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {
        int: ["id", "userId", "investId", "tradeCredit", "status"],
        Timestamp: ["buyTime"],
        BigDecimal: ["buyPrice", "sellPrice", "profitCount","profitStopPrice","loseStopPrice"]
    }
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.fastQueryParameters['eq_status'] = 1;

//排序
grid.sortParameter.columnId = ['desc_buyTime'];
grid.sortParameter.sortType = 1; //0 升序  1倒序


setInterval(function () {
    grid.refresh(true)
}, 2000);
//列表结束
//操作处理开始
var OperateHandle = function () {
    function _bindEvent() {

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['eq_status'] = 1;
            grid.fastQueryParameters['lk_user_id'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
        // qiangDan : _qiangDan(this)
    }
}();

/**
 * 抢单js
 * @param event
 * @private
 */
function _qiangDan(event) {
    //获取接受事件的元素
    // var button = $(event.relatedTarget);
    // //获取data 参数
    // var datano = button.data('no');
    var datano = $(event).attr("data-no");
    //获取列表框中的原始数据
    var gridData = grid.sortOriginalDatas[datano];
    var id = gridData.id;
    var investId = $('#userId').val();
    $.ajax({
        async: false,
        url: ncGlobal.adminRoot + 'order/scramble.json',
        type: "post",
        data: {id: id, investorId: investId},
        success: function (data) {
            if (data.code == 200) {
                //清除错误信息
                $(".alert-danger").remove();
                $("#addForm").psly().reset();
                $("#equityName").val(gridData.equityName);
                $("#equityCode").val(gridData.equityCode);
                $("#number").val(gridData.buyNumber);
                $("#buyPrice").val(gridData.buyPrice);
                $("#buyDealNumber").val('');
                $(".orderId").val(gridData.id);
                $("#buyDealPrice").val('');
                $("#addModal").modal("show");
            } else {
                $.ncAlert({content: data.message, autoCloseTime: 3});
            }
        },
        error: function (e) {
            alert("错误！！");
        }
    });
}
$("#copy-stock-code").zclip({
    path: '${pluginsRoot}zclip/ZeroClipboard.swf',
    copy : $("#equityCode").val(),
    afterCopy: function() {
        // console.log('copied');
        alert('Data in clipboard! Now you can paste it somewhere');
    }
});
$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});